﻿// From:
// WPBehaviorsLibrary
// http://aimeegurl.com/2010/03/18/panoramic-navigation-on-windows-phone-7-with-no-code/
//
using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Expression.Interactivity;

using System.Windows.Interactivity;

namespace BehaviorsLibrary
{
  
        public class FlickGestureTrigger : TriggerBase<UIElement>
        {
            public FlickGestureTrigger() { 
            
            }
            public FlickDirection Direction
            {
                get { return (FlickDirection)GetValue(DirectionProperty); }
                set { SetValue(DirectionProperty, value); }
            }

            // Using a DependencyProperty as the backing store for Direction.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty DirectionProperty =
                DependencyProperty.Register("Direction", typeof(FlickDirection), typeof(FlickGestureTrigger), new PropertyMetadata(FlickDirection.Right));

            
            private UIElement AssociatedUIElement
            {
                get { return (UIElement)this.AssociatedObject; }
            }

            /// <summary>
            /// Attach the appropriate events.
            /// </summary>
            protected override void OnAttached()
            {
                base.OnAttached();
                this.AssociatedUIElement.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(AssociatedUIElement_ManipulationCompleted);  
            }

            void AssociatedUIElement_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
            {
                if (e.IsInertial) {
                    var delta = e.TotalManipulation.Translation;
                    switch (Direction)
                    {
                        case FlickDirection.Up:
                            if (delta.Y > 0)
                                this.InvokeActions(e);
                            break;
                        case FlickDirection.Down:
                            if (delta.Y < 0)
                                this.InvokeActions(e);
                            break;
                        case FlickDirection.Left:
                            if (delta.X < 0)
                                this.InvokeActions(e);
                            break;
                        case FlickDirection.Right:
                            if (delta.X > 0)
                                this.InvokeActions(e);
                            break;
                        case FlickDirection.UpRight:
                            if (delta.Y > 0 && delta.X > 0)
                                this.InvokeActions(e);
                            break;
                        case FlickDirection.UpLeft:
                            if (delta.Y > 0 && delta.X < 0)
                                this.InvokeActions(e);
                            break;
                        case FlickDirection.DownRight:
                            if (delta.Y < 0 && delta.X > 0)
                                this.InvokeActions(e);
                            break;
                        case FlickDirection.DownLeft:
                            if (delta.Y < 0 && delta.X < 0)
                                this.InvokeActions(e);
                            break;
                        default:
                            break;
                    }
                }
            }


            /// <summary>
            /// Detach from the appropriate events.
            /// </summary>
            protected override void OnDetaching()
            {
                base.OnDetaching();

                this.AssociatedUIElement.ManipulationCompleted -= new EventHandler<ManipulationCompletedEventArgs>(AssociatedUIElement_ManipulationCompleted);  
          
            }



        }
        public enum FlickDirection
        { 
            Up, Down, Left, Right, UpRight, UpLeft, DownRight, DownLeft
        }

}
